﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WCS.Entities;

namespace Warehouse.Services.Repository
{
    public class Repository<T> : SimpleClient<T> where T : class, new()
    {
        
        public Repository(ISqlSugarClient context = null) : base(context)
        {
            Context = context;
        }

        #region 插入

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="entity">实体数据实例</param>
        /// <returns></returns>
        public virtual bool Add(T entity)
        {
            return base.Insert(entity);
        }


        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="entity">实体数据实例</param>
        /// <returns></returns>
        public virtual async Task<bool> AddAsync(T entity)
        {
            return await base.InsertAsync(entity);
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="entity">实体数据实例</param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T[] entity)
        {
            return await base.InsertRangeAsync(entity);
        }

        #endregion

        #region 更新

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override bool Update(T entity)
        {
            return base.Update(entity);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override async Task<bool> UpdateAsync(T entity)
        {
            return await base.UpdateAsync(entity);
        }



        #endregion

        #region 删除


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual bool DeleteById(int id)
        {
            return base.DeleteById(id);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual async Task<bool> DeleteByIdAsync(int id)
        {
            return await base.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual bool DeleteById(string id)
        {
            return base.DeleteById(id);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual async Task<bool> DeleteByIdAsync(string id)
        {
            return await base.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">主键数组或者唯一性字段数组</param>
        /// <returns></returns>
        public virtual new bool DeleteByIds(dynamic[] ids)
        {
            return base.DeleteByIds(ids);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public virtual async Task<bool> DeleteByIdAsync(dynamic[] ids)
        {
            return await base.DeleteByIdAsync(ids);
        }


        /// <summary>
        /// 根据条件批量删除
        /// </summary>
        /// <param name="func">删除条件</param>
        /// <returns></returns>
        public virtual new bool Delete(Expression<Func<T, bool>> func)
        {
            return base.Delete(func);
        }

        /// <summary>
        /// 根据条件批量删除
        /// </summary>
        /// <param name="func">删除条件</param>
        /// <returns></returns>
        public virtual new async Task<bool> DeleteAsync(Expression<Func<T, bool>> func)
        {
            return await base.DeleteAsync(func);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual new bool Delete(T entity)
        {
            return base.Delete(entity);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual new async Task<bool> DeleteAsync(T entity)
        {
            return await base.DeleteAsync(entity);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual new bool Delete(List<T> entities)
        {
            return base.Delete(entities);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual new async Task<bool> DeleteAsync(List<T> entities)
        {
            return await base.DeleteAsync(entities);
        }
        #endregion

        #region 查找


        /// <summary>
        /// 通过Id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> FindAsync(int id)
        {
            return await base.GetByIdAsync(id);
        }

        /// <summary>
        /// 通过Id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> FindAsync(string id)
        {
            return await base.GetByIdAsync(id);
        }

        /// <summary>
        /// 查找单个数据
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public virtual async Task<T> FindAsync(Expression<Func<T, bool>> func)
        {
            return await base.GetFirstAsync(func);
        }


        /// <summary>
        /// 查找所有的
        /// </summary>
        /// <returns></returns>
        public virtual List<T> Query()
        {
            return base.GetList();
        }

        /// <summary>
        /// 查找所有的
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync()
        {
            return await base.GetListAsync();
        }

        /// <summary>
        /// 根据条件查找多个
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func)
        {
            return await Context.Queryable<T>().Where(func).ToListAsync();
        }

        public virtual Task<bool> IsExistAsync(Expression<Func<T, bool>> func)
        {
            return base.IsAnyAsync(func);
        }

        public virtual bool IsExist(Expression<Func<T, bool>> func)
        {
            return base.IsAny(func);
        }

        public virtual List<T> Query(Expression<Func<T, bool>> func)
        {
            return Context.Queryable<T>().Where(func).ToList();
        }

        public virtual async Task<List<T>> QueryAsync(int page, int size, RefAsync<int> total)
        {
            return await Context.Queryable<T>()
                .ToPageListAsync(page, size, total);
        }

        public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> func, int page, int size, RefAsync<int> total)
        {
            return await Context.Queryable<T>()
                 .Where(func)
                 .ToPageListAsync(page, size, total);
        }

        public virtual async Task<List<T>> QueryAsync(List<IConditionalModel> conditionalModels, int page, int size, RefAsync<int> total)
        {
            return await Context.Queryable<T>()
               .Where(conditionalModels)
               .ToPageListAsync(page, size, total);
        }


        #endregion
    }
}
